/****************************************************************************/
/*																			*/
/*	Module:			jbistub.c												*/
/*																			*/
/*					Copyright (C) Altera Corporation 1997-2001				*/
/*																			*/
/*	Description:	Jam STAPL ByteCode Player main source file				*/
/*																			*/
/*					Supports Altera ByteBlaster hardware download cable		*/
/*					on Windows 95 and Windows NT operating systems.			*/
/*					(A device driver is required for Windows NT.)			*/
/*																			*/
/*					Also supports BitBlaster hardware download cable on		*/
/*					Windows 95, Windows NT, and UNIX platforms.				*/
/*																			*/
/*	Revisions:		1.1 fixed control port initialization for ByteBlaster	*/
/*					2.0 added support for STAPL bytecode format, added code	*/
/*						to get printer port address from Windows registry	*/
/*					2.1 improved messages, fixed delay-calibration bug in	*/
/*						16-bit DOS port, added support for "alternative		*/
/*						cable X", added option to control whether to reset	*/
/*						the TAP after execution, moved porting macros into	*/
/*						jbiport.h											*/
/*					2.2 added support for static memory						*/
/*						fixed /W4 warnings									*/
/*																			*/
/****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include "sys_gpio.hpp"
#include "altera.hpp"
#include "altera-jtag.hpp"
#include "sys_gpio_def.hpp"
#include "jbiexprt.hpp"

#include "data.hpp"
#include "ws.hpp"
uint8_t* file_ptr = nullptr;
uint32_t file_len = 0;
extern my_endpoint m_endpoint;

/************************************************************************
*
*		TDI -> 39 = PE0
*		TMS -> 40 = PE1
*		TCK -> 41 = PE2
*		TDO <- 42 = PE3
*/
bool trace = false;
void altera_message(websocketpp::connection_hdl hdl,char *message_text)
{
	printf("%s\n", message_text);
	fflush(stdout);
	text_t text;
				
	text.ctl = 0x02;
	text.type = 0x04;
	text.length_total = strlen(message_text)+1;
	text.number=0x00;
	text.length_data=strlen(message_text)+1;
	memcpy(text.data,message_text,strlen(message_text)+1);
	m_endpoint.socket_send(hdl,text);
}

void altera_export_int(char *key, int32_t value)
{
}

void altera_export_bool_array(char *key, uint8_t *data, int32_t count)
{
}
int altera_jtag_io(int tms, int tdi, int read_tdo)
{
	int tdo = 0;
//	printf("sdcsdcsdcdsc\n");
	if(tms)mt76x8_gpio_set_pin_value(TMS,1);
	else mt76x8_gpio_set_pin_value(TMS,0);
	//sleep(1);
	if(tdi)mt76x8_gpio_set_pin_value(TDI,1);
	else mt76x8_gpio_set_pin_value(TDI,0);
	//sleep(1);
	if(read_tdo)
	{
		if(mt76x8_gpio_get_pin(TDO))
			tdo=1;
		else
			tdo=0;
	}
	//sleep(1);
	mt76x8_gpio_set_pin_value(TCK,1);
	//sleep(1);
	mt76x8_gpio_set_pin_value(TCK,0);
	return (tdo);
}

void jbi_delay(long microseconds)
{
	usleep(microseconds);
}
bool ALTERA_Download(websocketpp::connection_hdl hdl,uint8_t *jbi_program,uint32_t length,int is_fpga)
{

	int32_t error_address = 0L;
	JBI_RETURN_TYPE crc_result = JBIC_SUCCESS;
	JBI_RETURN_TYPE exec_result = JBIC_SUCCESS;
	// unsigned short expected_crc = 0;
	// unsigned short actual_crc = 0;
	int exit_code = 0;
	int format_version = 0;
	//int reset_jtag = 1;

	char action_fpga[]="CONFIGURE" ;
	char action[]="PROGRAM";
	crc_result = altera_check_crc(jbi_program, length);

	if (crc_result != JBIC_SUCCESS)
		return false;
	printf("crc ok!\r\n");
	if(is_fpga)
	{
		exec_result = altera_execute(hdl,jbi_program, length, action_fpga, NULL, &error_address, &exit_code, &format_version);

		printf("exec ok!\r\n");
		printf("exec_result:%d\r\n",exec_result);
		printf("exit_code:%d\r\n",exit_code);
		if ((exec_result == JBIC_SUCCESS)&&(exit_code==0))
		{
			exec_result = altera_execute(hdl,jbi_program, length, action, NULL, &error_address, &exit_code, &format_version);
			if ((exec_result == JBIC_SUCCESS)&&(exit_code==0))
				return true;
			else
			{
				printf("fpga fail!\n");
				return true;
			}
		}
		else
			return false;
	}
	else
	{
		exec_result = altera_execute(hdl,jbi_program, length, action, NULL, &error_address, &exit_code, &format_version);
		printf("exec_result:%d\r\n",exec_result);
		printf("exit_code:%d\r\n",exit_code);
		printf("exec ok!\r\n");
		if ((exec_result == JBIC_SUCCESS)&&(exit_code==0))
		{
				return true;
		}
		else
			return false;
	}
}
